<?php
	
	App::import('Vendor', 'oauth');
	//require_once '../../src/contrib/apiCalendarService.php';
	//App::import('Vendor', 'apiCalendarService', array('file' =>'google_api_php'.DS.'contrib'.DS.'apiCalendarService.php'));
	class Calendar extends GapiAppModel {
		var $defaultParams = array();
		
/*
		function calendarList(){
			$cal = new apiCalendarService();
			$calList = $cal->calendarList->listCalendarList();
			print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
		}
*/
		function calendarList($params=NULL){
			$feed = 'https://www.googleapis.com/calendar/v3/users/me/calendarList/';
			$params = array_merge($this->defaultParams,$params);
			$result = $this->request('GET',$feed,$params);
			return $result;
		}
		
		function addEvent($params,$calendar){
			$calendar_id = $this->getCalendarId($params,$calendar);
			if($calendar_id){
				$feed = 'https://www.googleapis.com/calendar/v3/calendars/'.$calendar_id.'/events/quickAdd';
				$params = array_merge($this->defaultParams,$params);
				$result = $this->request('POST',$feed,$params);
				//debug($result);
				return $result;
			}
		}
		
		function getCalendarId($params,$calendar){
			$feed = 'https://www.googleapis.com/calendar/v3/users/me/calendarList/';
			$params = array_merge($this->defaultParams,$params);
			$result = $this->request('GET',$feed,$params);
			//debug($result);
			//$results = json_decode($result);
			$calendarId = '';
			foreach($result->items as $cal){
				$summary = $cal->summary;
				if($summary == $calendar){
					$calendarId = $cal->id;
				}
			}
			//debug($calendarId);
			return $calendarId;
		}
	}
?>
